package com.example.ziliao.ui.overlay

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.ziliao.R
import com.example.ziliao.utils.OptionGenerator
import com.example.ziliao.utils.OptionsHelper
import kotlin.random.Random
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.pow
import com.example.ziliao.utils.NumberGenerator
import com.example.ziliao.utils.ViewHelper

class OverlayFragment : Fragment() {
    private lateinit var btnStart: Button
    private lateinit var btnSum: Button
    private lateinit var btnAverage: Button
    private lateinit var tvQuestion: TextView
    private lateinit var tvNumbers: TextView
    private lateinit var optionsContainer: LinearLayout
    private lateinit var btnOptionA: Button
    private lateinit var btnOptionB: Button
    private lateinit var btnOptionC: Button
    private lateinit var btnOptionD: Button
    private lateinit var tvExactResult: TextView
    private var currentSum: Double = 0.0
    private var currentCount: Int = 0
    private var isAverageMode: Boolean = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_overlay, container, false)
        
        btnStart = root.findViewById(R.id.btnStart)
        btnSum = root.findViewById(R.id.btnSum)
        btnAverage = root.findViewById(R.id.btnAverage)
        tvQuestion = root.findViewById(R.id.tvQuestion)
        tvNumbers = root.findViewById(R.id.tvNumbers)
        optionsContainer = root.findViewById(R.id.optionsContainer)
        btnOptionA = root.findViewById(R.id.btnOptionA)
        btnOptionB = root.findViewById(R.id.btnOptionB)
        btnOptionC = root.findViewById(R.id.btnOptionC)
        btnOptionD = root.findViewById(R.id.btnOptionD)
        tvExactResult = root.findViewById(R.id.tvExactResult)

        btnStart.setOnClickListener {
            isAverageMode = Random.nextBoolean()
            resetOptions()
            generateAndDisplayNumbers()
        }

        btnSum.setOnClickListener {
            isAverageMode = false
            resetOptions()
            generateAndDisplayNumbers()
        }

        btnAverage.setOnClickListener {
            isAverageMode = true
            resetOptions()
            generateAndDisplayNumbers()
        }

        return root
    }

    private fun resetOptions() {
        // 重置选项状态
        OptionsHelper.resetOptions(
            btnOptionA,
            btnOptionB,
            btnOptionC,
            btnOptionD,
            optionsContainer,
            tvExactResult
        )
    }

    private fun generateAndDisplayNumbers() {
        // 随机选择生成5-10个数
        currentCount = Random.nextInt(5, 11)
        
        // 使用NumberGenerator生成数字
        val displayNumbers = NumberGenerator.generateControlledNumbers(currentCount)
        
        // 计算总和
        currentSum = if (isAverageMode) displayNumbers.sum() / currentCount else displayNumbers.sum()

        // 显示题干
        tvQuestion.text = if (isAverageMode) "求这${currentCount}个数的平均值" else "求总和"
        tvQuestion.visibility = View.VISIBLE

        // 计算范围
        val (lowerBound, upperBound) = OptionGenerator.calculateRange(currentSum)

        // 使用NumberGenerator格式化显示文本
        tvNumbers.text = NumberGenerator.formatDisplayText(displayNumbers)
        tvNumbers.visibility = View.VISIBLE
        generateOptions(currentSum)
    }

    private fun generateOptions(sum: Double) {
        // 使用OptionsHelper生成和显示选项
        OptionsHelper.generateAndDisplayOptions(
            correctAnswer = sum,
            useRangeOptions = true,
            btnOptionA = btnOptionA,
            btnOptionB = btnOptionB,
            btnOptionC = btnOptionC,
            btnOptionD = btnOptionD,
            optionsContainer = optionsContainer,
            tvExactResult = tvExactResult,
            resultPrefix = if (isAverageMode) "准确平均值" else "准确总和"
        )
    }
} 